package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.GrantChargeItemListQueryVO;
import com.ce.pms.report.vo.GrantChargeItemListReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：财务应收款分项明细表 Service
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军
 * Created on 2008-11-3
 * Edit author:
 * Edit on:
 * 修改原因:
 */
public class GrantChargeItemListService implements ICEReportDataSetService {

	private IReportDao reportDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg) throws CEReportException {
		GrantChargeItemListQueryVO queryVO = (GrantChargeItemListQueryVO) arg;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取列表数据  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private  List<GrantChargeItemListReportVO> getData(GrantChargeItemListQueryVO queryVO){

		List<Object[]> temp = reportDao.getGrantChargeItemList(queryVO);
		List<GrantChargeItemListReportVO> list = arrayToVo(temp);

		return list;
	}

	/**
	 * 功能描述:对象数组 转为 VO 数组  <br>
	 * @param temp
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<GrantChargeItemListReportVO> arrayToVo(List<Object[]> temp){
		if(BlankUtil.isBlank(temp)){
			return null;
		}

		List<GrantChargeItemListReportVO> list = new ArrayList<GrantChargeItemListReportVO>();
		for (Object[] obj : temp) {
			GrantChargeItemListReportVO reportVO = new GrantChargeItemListReportVO();
			reportVO.setHouseNumber((String)obj[1]);	//房号
			reportVO.setUserName((String)obj[3]);		//客户名称
			reportVO.setArea(setValue(obj[4]));		//面积
			reportVO.setAccounts((String)obj[5]);		//银行帐号
			reportVO.setActualThis(setValue(obj[6]));	//本月费用
			reportVO.setArrearagePay(setValue(obj[7]));//往月欠款
			reportVO.setOverheadExpenses(setValue(obj[8]));//物业管理费
			reportVO.setWaterPay(setValue(obj[9]));	//水费
			reportVO.setElectricityPay(setValue(obj[10]));//电费
			reportVO.setSewagePay(setValue(obj[11]));//污水费
			reportVO.setGarbagePay(setValue(obj[12]));	//垃圾费
			reportVO.setOnticUpkeep(setValue(obj[13]));	//本体维修基金
			reportVO.setUpkeepPay(setValue(obj[14]));		//维修费
			reportVO.setLatefee(setValue(obj[15]));		//滞纳金

			BigDecimal accountReceivable = new BigDecimal(0);
			if(!BlankUtil.isBlank( reportVO.getActualThis())){
				accountReceivable = accountReceivable.add(reportVO.getActualThis());
			}
			if(!BlankUtil.isBlank(reportVO.getArrearagePay())){
				accountReceivable = accountReceivable.add(reportVO.getArrearagePay());
			}
			if(!BlankUtil.isBlank(reportVO.getLatefee())){
				accountReceivable = accountReceivable.add(reportVO.getLatefee());
			}
			reportVO.setAccountReceivable(accountReceivable);//本月应收合计

			list.add(reportVO);
		}

		return list;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(GrantChargeItemListQueryVO queryVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyy年MM月");
		map.put("reportHeadStr","计费月份："+ computeMonth);
		map.put("printDate", new Date());
		String subentry = "";
		if("1".equals(queryVO.getSubentry())){
			subentry="本月费用";
		}else if("2".equals(queryVO.getSubentry())){
			subentry="往月欠款";
		}else{
			subentry="本月费用 + 往月欠款";
		}
		map.put("subentry", subentry);
		return map;
	}

	//传入的值必须是 BigDecimal 类型的,否则返回空    如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}

}
